public class CXGClosing
{
 
 /* 
 Class Name: CXGClosing
 Date: 14-April-2011  
 Requirement/Project Name: CXG   
 Requirement/Project Description: Agent Script Page which only shows when agent selects Scheduled Call Back 
 */
//All ProperTies and Variables Declared below 
Public String text1{get;set;}
Public String msgtext{get;set;}
Public Id acctid{get;set;} 
Boolean flag=false;
Public Event evnt;
Public Date datetime_start{get;set;}
Public DateTime datetime_end{get;set;}
public string taskdisposition1{get;set;}
Public string callreslution1{get;set;}
Public String callback{get;set;}
Public Id campid {get;set;}
Public Id cmid{get;set;}
Public CXG_Campaign_Accounts__c campaignmemb{get;set;}
Public Account contId{get;set;}
public CXGClosing(ApexPages.StandardController stdController) {
        evnt = (Event)stdController.getRecord();
        acctid=ApexPages.currentPage().getParameters().get('actid');
        cmid=ApexPages.currentPage().getParameters().get('cmid');
        campid=ApexPages.currentPage().getParameters().get('campaignid');
        campaignmemb=[Select Name,RecordTypeId from CXG_Campaign_Accounts__c where Id=:cmid];
        contId=[Select Primary_Contact_hidden__c from Account where Id=:acctid];        
        }
    
Public Event getEvent()
{
 return evnt;
}
public List<SelectOption> getDisposition() 
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--None--'));
        options.add(new SelectOption('Customer Contacted','Customer Contacted'));
        return options;
    }
public List<SelectOption> getCallResolution() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--None--'));
        Schema.DescribeFieldResult fieldResult = Task.Call_Resolutions__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for(Schema.PicklistEntry p : ple)
        options.add(new SelectOption(p.getValue(), p.getValue()));        
        return options;
               }
//Action Method for saving Dispostion ,Call Back and Notes on Accounts and campaign Members     
Public PageReference createNote1()
{
ID CxGEventRecordtypID = RecordTypeHelper.GetRecordTypeIdByDevName('Event', 'CXG_Record_Type');     
ID CxGTaskRecordTypeID = RecordTypeHelper.GetRecordTypeIdByDevName('Task', 'CXG_Task_Layout');
ID CxGCampaignMemberRecordTypeId= RecordTypeHelper.GetRecordTypeIdByDevName('CXG_Campaign_Accounts__c', 'Campaign_Member');
if(evnt.StartDateTime!=null)
{
 datetime_end=evnt.StartDateTime;
}
Notes__c notes = New Notes__c();
notes.Account__c=acctid;
notes.Notes__c=text1;
notes.Note__c='Salesforce';
notes.Disposition__c=taskdisposition1;
notes.Date__c=DateTime.Now();
Task tsk= New Task();
tsk.whatId=acctid;
tsk.Disposition__c=taskdisposition1;
tsk.ActivityDate=Date.Today();
tsk.Status='Completed';
tsk.WhoId=contId.Primary_Contact_hidden__c;
tsk.Attempt_Date__c=DateTime.Now();
tsk.IsContacted__c=True;
tsk.CXG_Account__c=True;
tsk.OwnerId=UserInfo.getUserId();
tsk.Campaign_Member_ID__c=campaignmemb.Name;
tsk.Priority='Normal';
tsk.RecordTypeId=CxGTaskRecordTypeID;
tsk.Subject=taskdisposition1;
tsk.Description=text1;
Task cmtsk= New Task();
cmtsk.whatId=cmid;
cmtsk.Disposition__c=taskdisposition1;
cmtsk.ActivityDate=Date.Today();
cmtsk.Attempt_Date__c=DateTime.Now();
cmtsk.Status='Completed';
cmtsk.IsContacted__c=True;
cmtsk.CXG_Account__c=True;
cmtsk.Priority='Normal';
cmtsk.OwnerId=UserInfo.getUserId();
cmtsk.Campaign_Member_ID__c=campaignmemb.Name;
cmtsk.Subject=taskdisposition1;
cmtsk.RecordTypeId=CxGTaskRecordTypeID;
cmtsk.Description=text1;
DateTime dtcheck=datetime_end.addSeconds(60); 
//Checking Not Null data before Submitting and also call back cannot ne prior to Today's date. 
if(text1=='' || taskdisposition1=='None' || evnt.StartDateTime== null)
{
Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.WARNING,'Error-"Please fill the Call Date,Notes and Disposition before Submitting."','Error-"Please fill the Call Date,Notes and Disposition before Submitting."' ));
}else if(dtcheck<DateTime.Now() )
{ 
 Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.WARNING,'Error-"Scheduled call back cannot be prior to today."','Error-"Scheduled call back cannot be prior to today."' ));
}
else
{
Insert notes;
Insert tsk;//Inserting Task on Account(Customer)
task insrtdTask = [select Server_URL__c from task where id = :tsk.id];
tsk.Campaign__c=insrtdTask.Server_URL__c+cmid;
update tsk;
Insert cmtsk;//Inserting Task on Campaign Members(For Reporting)
cmtsk.Campaign__c=insrtdTask.Server_URL__c+cmid;
update cmtsk;
notes.Campaign_Link__c=insrtdTask.Server_URL__c+cmid;
update notes;
CXG_Campaign_Accounts__c cm= New CXG_Campaign_Accounts__c(Id=cmid,Call_Back_Scheduled__c=True);
Event evt= New Event(Subject='Call Back Scheduled',Description=text1,Campaign_Member_ID__c=campaignmemb.Name,Call_Back_Scheduled__c=True,IsContacted__c=True,CXG_Account__c=True,RecordTypeId=CxGEventRecordtypID,Disposition__c=taskdisposition1,WhoId=contId.Primary_Contact_hidden__c,WhatId=acctid,StartDateTime=evnt.StartDateTime,IsReminderSet=True,ReminderDateTime=datetime_end.addMinutes(15),EndDateTime=datetime_end.addMinutes(30));
Insert evt;
evt.Campaign__c=insrtdTask.Server_URL__c+cmid;
update evt;
if(campaignmemb.RecordTypeId==CxGCampaignMemberRecordTypeId)
{
Update cm;
}

//For 60 Days CSS Functionality

if(taskdisposition1=='Customer Contacted'){
        Account accnt= New Account(Id=acctid);
        accnt.Contacted_Timestamp__c=Date.Today();
        update accnt;
         }

   
text1='';
evnt.StartDateTime=null;
msgtext='Success-"Your note,disposition and Call back event have been saved to the Campaign Member and Account record tied to this campaign member.Remember once completed to move on to next call in your agent queue or on the snapshot page by clicking “Next”';
}

taskdisposition1='None';
return null;
}  
}